/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package compiler.patch;

import java.util.Timer;
import java.util.TimerTask;

/**
 *
 * @author WL
 */
public class DownloadProgress extends javax.swing.JFrame {
    private long prevTime;
    private int prevAmt;
    private int currAmt;
    private TimerTask updater;
    private Timer timer;
    private int cooldown = 200;
    
    /**
     * Creates new form DownloadProgress
     */
    public DownloadProgress() {
        initComponents();
        
        updater = new TimerTask() {
            @Override
            public void run() {
                progress.setValue(currAmt);
                updateFields();
            }
        };
    }
    
    private void updateFields()
    {
        if (progress == null)
            return;
        
        int curr = currAmt;
        int total = progress.getMaximum();
        
        value.setText(curr + "/" + total);
        
        long currTime = System.currentTimeMillis();

        if (prevTime != 0 && (currTime - prevTime >= 50))
            speed.setText((curr - prevAmt) / (currTime - prevTime) + "kB/s");

        prevTime = currTime;
        prevAmt = curr;
    }
    
    public void setCurrent(int val)
    {
        currAmt = val;
        
        if (timer == null)
        {
            timer = new Timer();
            timer.schedule(updater, cooldown, cooldown);
        }
    }
    
    public void setTotal(int val)
    {
        progress.setMaximum(val);
    }
    
    public boolean hasCompleted()
    {
        return currAmt == progress.getMaximum();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        progress = new javax.swing.JProgressBar() {
            public void repaint()
            {
                updateFields();
                super.repaint();
            }
        };
        jLabel1 = new javax.swing.JLabel();
        speed = new javax.swing.JLabel();
        value = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Download Progress");

        jLabel1.setText("Downloading tools.jar...");

        speed.setText("0 kB/s");

        value.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(speed)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                        .addComponent(value, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(progress, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(value, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(speed)))
                .addGap(14, 14, 14)
                .addComponent(progress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JProgressBar progress;
    private javax.swing.JLabel speed;
    private javax.swing.JLabel value;
    // End of variables declaration//GEN-END:variables
}
